<template>
  <div class="sub-category">
    <!--
      使用v-if手动控制渲染的时机  只有有值的时候才正式渲染
      （正式渲染才意味着读取对象中的属性）
    -->
    <div class="container" v-if="subData && subData.parentId">
      <!-- 面包屑区域 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${subData.parentId}`">{{ subData.parentName }}</XtxBreadItem>
        <XtxBreadItem>{{ subData.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 筛选区 -->
      <div class="sub-filter">
        <!-- 规格  -->
        <div class="item" v-for="i in subData.saleProperties" :key="i.id">
          <div class="head">{{ i.name }}：</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="item in i.properties"
              :key="item.id"
              :class="{ active: item.selected === true }"
              @click="toggleSelect(i.properties, item, i)"
            >{{ item.name }}</a>
          </div>
        </div>
      </div>
      <!-- 排序区域 -->
      <div class="sub-sort">
        <div class="sort">
          <a
            v-for="item in sortTypeList"
            :key="item.type"
            href="javascript:;"
            @click="toggleSort(item.type)"
            :class="{ active: curSortType === item.type }"
          >{{ item.name }}</a>
        </div>
      </div>
      <div class="sort-list-container">
        <GoodsItem v-for="good in goodList" :key="good.id" :good="good" />
      </div>
    </div>
    <!-- 加载更多组件 -->
    <XtxLoad @load="loadNext" :is-finished="isFinished" :is-loading="isLoading"/>
  </div>
</template>

<script>
import GoodsItem from '@/views/home/components/home-goods'
import { useLoadFilterData } from './service/useLoadFilterData'
import { useLoadListData } from './service/useLoadListData'
export default {
  components: {
    GoodsItem
  },
  setup () {
    // setup: 组合式API 这里函数中应该放置的由各个逻辑函数执行之后的返回值(数据和方法这些数据和方法都是需要在模板中使用的)
    const { subData } = useLoadFilterData()
    const {
      toggleSelect,
      toggleSort,
      sortTypeList,
      goodList,
      curSortType,
      loadNext,
      isFinished,
      isLoading
    } = useLoadListData()

    return {
      subData,
      toggleSelect,
      toggleSort,
      curSortType,
      sortTypeList,
      goodList,
      loadNext,
      isFinished,
      isLoading
    }
  }
}
</script>

<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      word-break: break-all;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
.list-container {
  margin-top: 20px;
  padding: 0 20px;
  background: #fff;
}
.sort-list-container {
  display: flex;
  flex-wrap: wrap;
}
</style>
